package com.example.demo.controller;

import com.example.demo.common.ResponseMessage;
import com.example.demo.pojo.Cart;
import com.example.demo.pojo.Customer;
import com.example.demo.service.ICartService;
import com.example.demo.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart/")
public class CartController {
    @Autowired
    private ICartService iCartService;
    @Autowired
    private ICustomerService iCustomerService;

    /**
     * 购物车列表
     */
    @RequestMapping("list.do")
    public ResponseMessage list(HttpSession session) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
            int userId = user.getId();
            msg = iCartService.getCartList(userId);
            msg = new ResponseMessage(0, null, msg.getData());
            return msg;
        } else {
            msg = new ResponseMessage(10, "用户未登录，请登录");
            return msg;
        }
    }

    /**
     * 添加购物车
     */
    @RequestMapping("add.do")
    public ResponseMessage register(HttpSession session, int productId, int count) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
            int id = user.getId();
            iCartService.addCart(productId, count, id);
            msg = new ResponseMessage(0, "加入购物车成功", null);
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }

    /**
     * 更新购物车
     */
    @RequestMapping("update.do")
    public ResponseMessage updateByCart(HttpSession session, int productId, int quantity) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        int id = user.getId();
        iCartService.updateByCart(productId, quantity, id);
        ResponseMessage msg = null;
        if (user != null) {
            msg = new ResponseMessage(0, "更新信息成功");
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }
    /**
     * 移除购物车某个产品
     */
    @RequestMapping("delete_product.do")
    public ResponseMessage deleteproduct(HttpSession session,Integer product_id) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        int id = user.getId();
        ResponseMessage msg = null;
        if (user != null) {
            int res =  iCartService.deleteProduct(product_id);
            msg = new ResponseMessage(0, "移除购物车产品成功");
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }
    /**
     * 购物车选中某个商品
     */
    @RequestMapping("select.do")
    public ResponseMessage updateByChecked(HttpSession session,Integer product_id) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
       //int id = user.getId();
        ResponseMessage msg = null;
        if (user != null) {
            iCartService.updateByChecked(product_id);
            msg = new ResponseMessage(0, "选中成功");
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }
    /**
     * 购物车取消选中某个商品
     */
    @RequestMapping("un_select.do")
    public ResponseMessage updateByNOChecked(HttpSession session,Integer product_id) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
            iCartService.updateByNOChecked(product_id);
            msg = new ResponseMessage(0, "取消成功");
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }
    /**
     * 查询在购物车里的产品数量
     */
    @RequestMapping("get_cart_product_count.do")
    public ResponseMessage selectNum(HttpSession session,Integer user_id) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
            List<Cart> cartlist = iCartService.selectNum(user_id);
            msg = new ResponseMessage(0, "成功", cartlist);
        } else {
            msg = new ResponseMessage(10, "出现异常");
        }
        return msg;
    }


    /**
     * 购物车全选
     */
    @RequestMapping("select_all.do")
    public ResponseMessage updateBll(HttpSession session,Integer user_id) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
           int cartlist = iCartService.updateBll(user_id);
            msg = new ResponseMessage(0, "全选成功", cartlist);
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }


    /**
     * 购物车全取消
     */
    @RequestMapping("un_select_all.do")
    public ResponseMessage updateNOBll(HttpSession session,Integer user_id) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
            int cartlist = iCartService.updateNOBll(user_id);
            msg = new ResponseMessage(0, "全取消成功", cartlist);
        } else {
            msg = new ResponseMessage(10, "用户未登录,请登录");
        }
        return msg;
    }
}